<?php

require_once("View/View/LoggedInView.php");
require_once("Database/Model/WorkoutUpdateDAL.php");
require_once("Database/Model/MyWorkoutDAL.php");
require_once("WorkoutUpdates/Model/WorkoutUpdateModel.php");

class WorkoutUpdateView
{
	/**
	*@var string 
	*/
	private static $add ="add";
	/**
	*@var string 
	*/
	private static $added = "added";	
	/**
	*@var string 
	*/
	private static $edit = "edit";
	/**
	*@var string 
	*/
	private static $edited = "edited";
	/**
	*@var string 
	*/
	private static $remove = "remove";

	/**
	*@var LoggedInView 
	*/
	private $loggedInView;
	/**
	*@var UpdateModel 
	*/
	private $updateModel;
	/**
	*@var MyWorkoutDAL 
	*/
	private $myWorkoutDAL;
	/**
	*@var WorkoutUpdateDAL 
	*/
	private $workoutUpdateDAL;

	/**
	*@var string 
	*/
	private static $workout = "WorkoutUpdateView::workout";
	/**
	*@var string 
	*/
	private static $workoutUpdateTitle = "WorkoutUpdateView::newWorkoutUpdateTitle";
	/**
	*@var string 
	*/
	private static $workoutUpdateComment = "WorkoutUpdateView::newWorkoutUpdateComment";

	/**
	*@var string css id
	*/
	private static $nameTextField = "NameTextField"; 
	/**
	*@var string css id
	*/
	private static $bloggPostTextBox = "BloggPostTextBox";
	/**
	*@var string css id
	*/
	private static $formSubmitButton = "FormSubmitButton";

	public function __construct()
	{
		$this->loggedInView = new LoggedInView();
		$this->myWorkoutDAL = new MyWorkoutDAL();
		$this->updateModel = new WorkoutUpdateModel();
		$this->workoutUpdateDAL = new workoutUpdateDAL();
	}

	/**
	*@return string html
	*/
	public function getWorkoutUpdatePage()
	{
		$html = $this->getWorkoutUpdatePageHeader();
		$html .= $this->getWorkoutUpdatePageBody();
		$html .= $this->getWorkoutUpdatePageFooter();
		return $html;
	}

	/**
	*@return string html
	*/
	private function getWorkoutUpdatePageHeader()
	{
		$header = $this->loggedInView->getLoggedInHeader();
		return $header;
	}

	/**
	*@return string html
	*/
	private function getMenu()
	{
		$html = $this->loggedInView->getLoggedInMenu();
	    return $html;
	}

	/**
	*@return string html
	*/
	private function getWorkoutUpdatePageFooter()
	{
		$footer = $this->loggedInView->getFooter();
		return $footer;
	}


	/**
	*@return bool
	*/
	public function getIfRemove()
	{
		if(isset($_GET[self::$remove]))
		{
			return true;
		}
		return false;
	}

	/**
	*@return int
	*/
	public function getIdToRemove()
	{
		if(isset($_GET[self::$remove]) && $_GET[self::$remove] != null)
		{
			return $_GET[self::$remove];
		}
		return 0;
	}



	/**
	*@return bool
	*/
	public function getIfAdded()
	{
		if(isset($_GET[self::$added]))
		{
			return true;
		}
		return false;
	}

	/**
	*@return bool
	*/
	private function getIfAdd()
	{
		if(isset($_GET[self::$add]))
		{
			return true;
		}
		return false;
	}




	/**
	*@return bool
	*/
	public function getIfEdit()
	{
		if(isset($_GET[self::$edit]))
		{
			return true;
		}
		return false;
	}

	/**
	*@return bool
	*/
	public function getIfEdited()
	{
		if(isset($_GET[self::$edited]))
		{
			return true;
		}
		return false;
	}

	
	/**
	*@return int
	**/
	public function getIdToEdit()
	{
		$id = $_GET[self::$edit];

		if($id == null)
		{
			$id = $this->updateModel->getIdToEdit();
		}
		else
		{
			$this->updateModel->setIdToEdit($id);
		}
		return $id;
	}


	/**
	*@return string
	*/
	public function getWorkoutID()
	{
		if(isset($_POST[self::$workout]))
		{
			return $_POST[self::$workout];
		}
		return "";
	}

	/**
	*@return string
	*/
	public function getWorkoutUpdateTitle()
	{
		if(isset($_POST[self::$workoutUpdateTitle]))
		{
			return $_POST[self::$workoutUpdateTitle];
		}
		return "";
	}

	/**
	*@return string
	*/
	public function getWorkoutUpdateComment()
	{
		if(isset($_POST[self::$workoutUpdateComment]))
		{
			return $_POST[self::$workoutUpdateComment];
		}
		return "";
	}

	/**
	*@return string
	*/
	private function getMessage()
	{
		if($this->updateModel->getErrorMessage() != null)
		{
			return "<div id='ErrorMessageDiv'><p>" . $this->updateModel->getErrorMessage() . "</p></div>";
		}
		elseif($this->updateModel->getSuccessMessage() != null)
		{
			return "<div id='SuccessMessageDiv'><p>" . $this->updateModel->getSuccessMessage() . "</p></div>";
		}
		return "";
	}



	/**
	*@param bool
	*@return string
	*/
	private function getUpdateForm($edit = false)  
	{
		$title;  $comment;  $button = "Skapa";  $action = self::$added;
		if($edit)
		{ 
			$updateToEdit = $this->workoutUpdateDAL->getUpdates($this->getIdToEdit());
			$comment = $updateToEdit->getComment();
			$title = $updateToEdit->getTitle();
			$action = self::$edited;
			$button = "Redigera";
		}
		else
		{
			$title = $this->getWorkoutUpdateTitle();
			$comment = $this->getWorkoutUpdateComment();
		}

		$html = "
		<div id='CreateUpdateFormDiv' >
			<a href='MyWorkouts.php'>tillbaka</a>
			<form action='?".$action."' method='post' enctype='multipart/form-data'>
				<fieldset> 
					<legend>Lägg till övningar</legend>
					<label for='". self::$nameTextField . "'>Titel :</label>
					<input type='text' size='40' name='" . self::$workoutUpdateTitle . "' id='".self::$nameTextField."' value='$title' />
						
					<label for='" . self::$bloggPostTextBox . "' >Kommentar :</label>
					<textarea rows='4' cols='50' name='" . self::$workoutUpdateComment . "' id='" . self::$bloggPostTextBox . "'>$comment</textarea>
					<div class='musclegroupDropdown'> <label for='workoutsDropdown'>Välj vilket träningspass du vill kommentera</label> 
					<select id='workoutsDropdown' name='" . self::$workout . "' >";
					
		$workouts = $this->myWorkoutDAL->getWorkouts();
		foreach($workouts as $wo)
		{
			$html .= '<option value='.$wo->getID().'>'.$wo->getName().'</option>';
		}

		$html .= "</div></select>
					<input type='submit' name='' id='" . self::$formSubmitButton . "' value='$button' />
				</fieldset>
			</form>
		</div>";
			
		return $html;
	}
	
	/**
	*@return string
	*/
	private function getWorkoutUpdatePageBody()
	{
		$html = $this->getMenu();
		$html .= $this->getMessage();
		
		if($this->getIfAdd() || $this->getIfAdded())
	  	{
	  		$html .= $this->getUpdateForm();
	  	}
	  	else if($this->getIfEdit() || $this->getIfEdited()) 
	  	{
	  		$html .= $this->getUpdateForm(true); 
	  	}
	  	else
	  	{
	  		$html .= '<div id="workoutUpdateContainer">
				  	    <ul>
				  	    	<li>
								<a href="?'.self::$add.'">Ny Uppdatering</a>
							</li>
						</ul>
				  </div>';

	  		$html .= $this->getAllUpdatesHtml();
	  	}
		return $html; 
	}


	/**
	*@return string
	*/
	private function getAllUpdatesHtml()
	{
		$href; $woName;
		$html="<div id='UpdatesDiv'><h3>Senaste uppläggen.</h3>";
		$allUpdates = $this->getAllUpdates();

		foreach($allUpdates as $update)
		{	
			$workout = $this->myWorkoutDAL->getWorkouts($update->getWorkoutID());
			$href = 0; 

			if($workout != null)
			{
				$href = $workout->getID(); 
			}
			
			$html .= '<div id="AWorkoutUpdateDiv">
						<a class="editorremove" href=?edit='.$update->getUpdateID().'>Redigera</a>
						<a class="editorremove" href=?remove='.$update->getUpdateID().'>Ta bort</a>
						<h2>'.$update->getTitle().'</h2>
						<div id"updateComment">
						<p>'. $update->getComment().'</p>
						</div>
						<a id="UpdateWorkout" href = MyWorkouts.php?read='.$href.'> Träningsupplägget </a>
						<div id="dateDiv"><p>'. $update->getDate().'</p></div>	
			</div>'; 
		}
		$html .= "</div>";
		return $html;
	}


	/**
	*@param int
	*@return string
	*/
	private function getAllUpdates($id = null)
	{
		$allUpdates = $this->workoutUpdateDAL->getUpdates($id);
		return $allUpdates;
	}



}


